package models

import (
	"github.com/beego/beego/v2/adapter/logs"
	"github.com/beego/beego/v2/client/orm"
	beego "github.com/beego/beego/v2/server/web"
	_ "github.com/mattn/go-sqlite3"
	"time"
)

type BModel struct {
	ID        uint64    `orm:"column(id);pk;auto" json:"id" form:"id"`                 // 主键
	CreatedAt time.Time `orm:"column(created_at)" json:"created_at" form:"created_at"` // 创建时间
	UpdatedAt time.Time `orm:"column(updated_at)" json:"updated_at" form:"updated_at"` // 更新时间
	CreatedBy uint64    `orm:"column(created_by)" json:"created_by" form:"created_by"` // 创建人
	UpdatedBy uint64    `orm:"column(updated_by)" json:"updated_by" form:"updated_by"` // 更新人
}

func init() {

	//orm.RegisterDriver("mysql",
	//	orm.DRMySQL)
	//
	//conn :=  "root:1308956271@tcp(127.0.0.1:3306)/cmdb?charset=utf8"
	////注册数据库连接
	//orm.RegisterDataBase("default", "mysql", conn)
	conn, err := beego.AppConfig.String("db")
	if err != nil {
		logs.Error("[db-err]:", err)
		return
	}

	//
	if beego.BConfig.RunMode == beego.DEV {
		//开发模式-开启调试
		orm.Debug = true

	}

	//orm.DefaultTimeLoc = loc
	if err = orm.RegisterDriver("sqlite3", orm.DRSqlite); err != nil {
		panic(err.Error())
	}
	if err = orm.RegisterDataBase("default", "sqlite3", conn); err != nil {
		panic(err.Error())
	}
	//注册模型
	orm.RegisterModel(NewUser(), NewServerInfo(), NewDashboard(),
		NewMenu(), NewSetting(), NewRole(), NewRoleMenu(), NewLog(),
		NewForm(), NewFormOption(), NewWebSite())
	//loc, _ := time.LoadLocation("Asia/Shanghai")
	//设置时区
	if err = orm.SetDataBaseTZ("default", time.Local); err != nil {
		panic(err.Error())
	}

}
